package commons.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

/**
 * 秘钥工具
 * @author Administrator
 *
 */
public class KeyUtil {

	public static void main(String[] args) throws Exception{
//		long start = System.currentTimeMillis();
//		for(int i=0; i<10000; i++){
//			genAESKey();
//		}
//		long end = System.currentTimeMillis();
//		System.err.println(end - start);
//		System.err.println("key : " + genAESKey());
//		System.err.println("Kamuy_IVPassword".length());
//		System.err.println("iv = " + genIV());
		System.err.println("HmacMD5 = " + genMD5Key());
		
//		StringBuilder key = new StringBuilder();
//		for(int i=0; i<32; i++){
//			key.append("0");
//		}
//		System.err.println(key.toString());
//		System.err.println(key.toString().length());
		
		//C69E0BB651B8E5397E658D4B0746DD57
		//D1141CFA6618FD0C5BE1D01273EA9505
		//EFE779FD6A3A61A5B2E9638ADA50FE9D
	}
	
	private static final String FIX_KEY = "00000000000000000000000000000000";
	public static String genIV() throws Exception{
		SecretKey _password = new SecretKeySpec(new HexBinaryAdapter().unmarshal(FIX_KEY), "AES");
		
		Cipher ciper = Cipher.getInstance("AES/CBC/PKCS5Padding");
		ciper.init(Cipher.ENCRYPT_MODE, _password);
		
		return new HexBinaryAdapter().marshal(ciper.getIV());
	}
	
	/**
	 * 生成128位秘钥
	 * @param algorithm
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	private static String genKEY(String algorithm) throws NoSuchAlgorithmException{
		KeyGenerator kg = KeyGenerator.getInstance(algorithm);
		kg.init(128);// 要生成多少位，只需要修改这里即可128, 192或256
		SecretKey sk = kg.generateKey();
		byte[] data = sk.getEncoded();
		
		return new HexBinaryAdapter().marshal(data);
	}
	
	public static String genAESKey() throws NoSuchAlgorithmException{
		return genKEY("AES");
	}
	
	public static String genHMACSHA1Key() throws NoSuchAlgorithmException{
		return genKEY("HmacSHA1");
	}
	
	public static String genMD5Key() throws NoSuchAlgorithmException{
		return genKEY("HmacMD5");
	}
	
	/**
	 * 生成文件MD5
	 * @param file
	 * @return
	 * @throws IOException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static String genMd5ByFile(File file) throws IOException, NoSuchAlgorithmException {
		String value = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(byteBuffer);
			BigInteger bi = new BigInteger(1, md5.digest());
			value = bi.toString(16);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}
}
